<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(B卷,100分)- 数列描述（Java & JS & Python）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>有一个数列a[N] (N&#61;60)&#xff0c;从a[0]开始&#xff0c;每一项都是一个数字。数列中a[n&#43;1]都是a[n]的描述。其中a[0]&#61;1。规则如下&#xff1a;</p> 
<ul><li>a[0]:1</li><li>a[1]:11(含义&#xff1a;其前一项a[0]&#61;1是1个1&#xff0c;即“11”。表示a[0]从左到右&#xff0c;连续出现了1次“1”&#xff09;</li><li>a[2]:21(含义&#xff1a;其前一项a[1]&#61;11&#xff0c;从左到右&#xff1a;是由两个1组成&#xff0c;即“21”。表示a[1]从左到右&#xff0c;连续出现了两次“1”)</li><li>a[3]:1211(含义&#xff1a;其前一项a[2]&#61;21&#xff0c;从左到右&#xff1a;是由一个2和一个1组成&#xff0c;即“1211”。表示a[2]从左到右&#xff0c;连续出现了1次“2”&#xff0c;然后又连续出现了1次“1”)</li><li>a[4]:111221(含义&#xff1a;其前一项a[3]&#61;1211&#xff0c;从左到右&#xff1a;是由一个1、一个2、两个1组成&#xff0c;即“111221”。表示a[3]从左到右&#xff0c;连续出现了1次“1”&#xff0c;连续出现了1次“2”&#xff0c;连续出现了两次“1”)</li></ul> 
<p>请输出这个数列的第n项结果&#xff08;a[n]&#xff0c;0≤n≤59&#xff09;。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>数列的第n项(0≤n≤59)</p> 
<p>4</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>数列的内容</p> 
<p>111221</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:76px;">输入</td><td style="width:422px;">4</td></tr><tr><td style="width:76px;">输出</td><td style="width:422px;">111221</td></tr><tr><td style="width:76px;">说明</td><td style="width:422px;">无</td></tr></tbody></table> 
<p></p> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>这题是一个典型的动态规划题目&#xff0c;因为它存在明显的&#xff1a;下一个阶段状态依赖于上一个阶段状态&#xff0c;存在状态转移。</p> 
<p>这题难点在于找状态转移方程&#xff0c;即上一个状态与上一个状态之间的变化关系&#xff0c;按照题目意思应该是一种描述关系。即下一个状态是对于上一个状态的描述。</p> 
<p>我这边实现&#xff1a;比如上一个状态是1211&#xff0c;则将其转为字符数组arr&#xff0c;取出第0个字符缓存到变量val中&#xff0c;并定义一个变量count&#xff0c;用于记录val字符出现的次数。</p> 
<p>然后开始遍历字符数组&#xff0c;从第1个开始遍历&#xff0c;比较arr[0]和arr[1]&#xff0c;如果相同&#xff0c;则count&#43;&#43;&#xff0c;如果不同&#xff0c;则输出描述count &#43; &#34;&#34; &#43; val&#xff0c;然后将arr[1]作为新的val&#xff0c;并且count重置为1&#xff0c;接着比较arr[1]和arr[2]&#xff0c;逻辑如上。</p> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JS算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

rl.on(&#34;line&#34;, (line) &#61;&gt; {
  const n &#61; parseInt(line);
  console.log(getSeq(n));
});

/* 算法逻辑 */
function getSeq(n) {
  let base &#61; &#34;1&#34;;
  for (let i &#61; 1; i &lt;&#61; n; i&#43;&#43;) {
    base &#61; describe(base);
  }
  return base;
}

function describe(seq) {
  let res &#61; [];

  let count &#61; 1;
  let val &#61; seq[0];
  for (let i &#61; 1; i &lt; seq.length; i&#43;&#43;) {
    if (seq[i] &#61;&#61;&#61; seq[i - 1]) {
      count&#43;&#43;;
    } else {
      res.push(count);
      res.push(val);
      count &#61; 1;
      val &#61; seq[i];
    }
  }
  res.push(count);
  res.push(val);
  return res.join(&#34;&#34;);
}
</code></pre> 
<p></p> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);
    int n &#61; sc.nextInt();
    System.out.println(getSeq(n));
  }

  public static String getSeq(int n) {
    String base &#61; &#34;1&#34;;
    for (int i &#61; 1; i &lt;&#61; n; i&#43;&#43;) {
      base &#61; describe(base);
    }
    return base;
  }

  public static String describe(String seq) {
    StringBuilder sb &#61; new StringBuilder();

    int count &#61; 1;
    char val &#61; seq.charAt(0);

    for (int i &#61; 1; i &lt; seq.length(); i&#43;&#43;) {
      if (seq.charAt(i) &#61;&#61; seq.charAt(i - 1)) {
        count&#43;&#43;;
      } else {
        sb.append(count).append(val);
        count &#61; 1;
        val &#61; seq.charAt(i);
      }
    }
    sb.append(count).append(val);
    return sb.toString();
  }
}
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
n &#61; int(input())


def describe(seq):
    res &#61; []

    count &#61; 1
    val &#61; seq[0]

    for i in range(1, len(seq)):
        if seq[i] &#61;&#61; seq[i - 1]:
            count &#43;&#61; 1
        else:
            res.append(count)
            res.append(val)
            count &#61; 1
            val &#61; seq[i]

    res.append(count)
    res.append(val)
    return &#34;&#34;.join(map(str, res))


# 算法入口
def getResult():
    base &#61; &#34;1&#34;
    for i in range(1, n &#43; 1):
        base &#61; describe(base)
    return base


# 算法调用
print(getResult())
</code></pre>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>